import 'package:flutter/material.dart';
import 'package:flutter_trip/model/common_model.dart';
import 'package:flutter_trip/widget/webview.dart';

class SubNav extends StatelessWidget {
  const SubNav({Key? key, required this.subNavList}) : super(key: key);

  final List<CommonModel> subNavList;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(7))),
      child: Padding(
        padding: EdgeInsets.all(7),
        child: _items(context),
      ),
    );
  }

  _items(BuildContext context) {
    List<Widget> items = [];

    if (subNavList == null) return items;

    int spec = (subNavList.length / 2 + 0.5).toInt();

    subNavList.forEach((element) {
      items.add(_item(context, element));
    });

    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: items.sublist(0, spec),
        ),
        Padding(
            padding: EdgeInsets.only(
              top: 10,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: items.sublist(spec, subNavList.length),
            ))
      ],
    );
  }

  Widget _item(BuildContext context, CommonModel element) {
    return Expanded(
        flex: 1,
        child: GestureDetector(
          onTap: () {
            Navigator.push(context, MaterialPageRoute(builder: (context)=>
                HiWebView(
                  url: element.url,
                  statusBarColor: element.statusBarColor,
                  hideAppBar: element.hideAppBar,
                )
            ));
          },
          child: Column(
            children: [
              Image.network(
                element.icon!,
                width: 18,
                height: 18,
              ),
              Padding(padding: EdgeInsets.only(top: 7),child:  Text(
                element.title!,
                style: TextStyle(fontSize: 12),
              ),)

            ],
          ),
        ));
  }
}
